package com.myhouse.application.job.job;

import com.myhouse.common.exception.MyException;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时任务job
 */
@Slf4j
@Component
public class TestJob implements Job {

    /**
     * 倒计时时间
     */
    @Value("${countdown.date}")
    private String countdownDate;

    /**
     * 倒计时时间
     */
    @Value("${countdown.endtime}")
    private String countdownEndTime;

    /**
     * 倒计时时间
     */
    @Value("${countdown.starttime}")
    private String countdownStartTime;

    /**
     * test
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {

            String targetStartDateStr = countdownDate + " " + countdownStartTime;
            String targetEndDateStr = countdownDate + " " + countdownEndTime;
            Date targetStartDate = sdf.parse(targetStartDateStr);
            Date targetEndDate = sdf.parse(targetEndDateStr);
            long currentTime = currentDate.getTime();
            long targetStartTime = targetStartDate.getTime();
            long targetEndTime = targetEndDate.getTime();
            if (targetStartTime > targetEndTime) {
                throw new MyException("上班开始时间在上班结束时间之后");
            }
            if (currentTime <= targetStartTime) {
                printRestTime(1, currentTime, targetStartTime);
            } else if (currentTime > targetStartTime && currentTime <= targetEndTime) {
                printRestTime(2, currentTime, targetEndTime);
            } else {
                log.info("已过下班时间");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private void printRestTime(int mode, long currentTime, long targetTime) {
        String str = "";
        if (mode == 1) {
            str = "距离上班还剩: ";
        } else {
            str = "距离下班还剩: ";
        }
        long dayDiff = (targetTime - currentTime) /(86400 * 1000);
        long dayRest = (targetTime - currentTime) % (86400 * 1000);
        long hourDiff = dayRest / (3600 * 1000);
        long hourRest = dayRest % (3600 * 1000);
        long minuteDiff = hourRest / (60 * 1000);
        long minuteRest = hourRest % (60 * 1000);
        long secondDiff = minuteRest / (1 * 1000);
        if (dayDiff == 0 && hourDiff == 0 && minuteDiff == 0 && secondDiff == 0) {
            if (mode == 1) {
                str = "上班啦！";
            } else {
                str = "下班啦！";
            }
            log.info(str);
            return;
        }
        if (dayDiff > 0) {
            str += (dayDiff < 10 ? ("0" + dayDiff) : dayDiff) + "天";
        }
        if (hourDiff > 0) {
            str += (hourDiff < 10 ? ("0" + hourDiff) : hourDiff) + "小时";
        }
        if (minuteDiff > 0) {
            str += (minuteDiff < 10 ? ("0" + minuteDiff) : minuteDiff) + "分钟";
        }
        if (secondDiff > 0) {
            str += (secondDiff < 10 ? ("0" + secondDiff) : secondDiff) + "秒";
        }
        log.info(str);
    }
}
